package com.css.zfzx.fxpg.modules.casulaty.offline.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.fxpg.common.utils.BeanUtil;
import com.css.zfzx.fxpg.modules.casulaty.offline.repository.FxCasulatyOfflineNativeRepository;
import com.css.zfzx.fxpg.modules.casulaty.offline.repository.FxCasulatyOfflineRepository;
import com.css.zfzx.fxpg.modules.casulaty.offline.repository.entity.FxCasulatyOfflineEntity;
import com.css.zfzx.fxpg.modules.casulaty.offline.service.FxCasulatyOfflineService;
import com.css.zfzx.fxpg.modules.casulaty.offline.viewobjects.FxCasulatyOfflineQueryParams;
import com.css.zfzx.fxpg.modules.casulaty.offline.viewobjects.FxCasulatyOfflineVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author leon
 * @date 2020-11-16
 */
@Service
public class FxCasulatyOfflineServiceImpl implements FxCasulatyOfflineService {

	@Autowired
    private FxCasulatyOfflineRepository fxCasulatyOfflineRepository;
    @Autowired
    private FxCasulatyOfflineNativeRepository fxCasulatyOfflineNativeRepository;

    @Override
    public JSONObject queryFxCasulatyOfflines(FxCasulatyOfflineQueryParams queryParams, int curPage, int pageSize) {
        Page<FxCasulatyOfflineEntity> fxCasulatyOfflinePage = this.fxCasulatyOfflineNativeRepository.queryFxCasulatyOfflines(queryParams, curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(fxCasulatyOfflinePage);
        return jsonObject;
    }


    @Override
    public FxCasulatyOfflineEntity getFxCasulatyOffline(String id) {
        FxCasulatyOfflineEntity fxCasulatyOffline = this.fxCasulatyOfflineRepository.findById(id).orElse(null);
         return fxCasulatyOffline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FxCasulatyOfflineEntity saveFxCasulatyOffline(FxCasulatyOfflineEntity fxCasulatyOffline) {
        String uuid = UUIDGenerator.getUUID();
        fxCasulatyOffline.setId(uuid);
        fxCasulatyOffline.setCreateUser(PlatformSessionUtils.getUserId());
        fxCasulatyOffline.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.fxCasulatyOfflineRepository.save(fxCasulatyOffline);
        return fxCasulatyOffline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FxCasulatyOfflineEntity updateFxCasulatyOffline(FxCasulatyOfflineEntity fxCasulatyOffline) {
        fxCasulatyOffline.setUpdateUser(PlatformSessionUtils.getUserId());
        fxCasulatyOffline.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        String id = fxCasulatyOffline.getId();
        FxCasulatyOfflineEntity one = fxCasulatyOfflineRepository.findById(id).get();
        BeanUtil.copyPropertiesIgnoreNull(fxCasulatyOffline, one);
        this.fxCasulatyOfflineRepository.save(one);
        return fxCasulatyOffline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFxCasulatyOfflines(List<String> ids) {
        List<FxCasulatyOfflineEntity> fxCasulatyOfflineList = this.fxCasulatyOfflineRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(fxCasulatyOfflineList) && fxCasulatyOfflineList.size() > 0) {
            for(FxCasulatyOfflineEntity fxCasulatyOffline : fxCasulatyOfflineList) {
                this.fxCasulatyOfflineRepository.delete(fxCasulatyOffline);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

}
